<?PHP
include("header_simple.php");
$nFiltro=0;
$cPaterno = limpia_usuario($cPaterno);
$cMaterno = limpia_usuario($cMaterno);
$cNombre = limpia_usuario($cNombre);
$curp 	= limpia_usuario($curp);
$cFolio = limpia_usuario($cFolio);
if(strlen($cPaterno) > 2) $nFiltro=1; 
if(strlen($cMaterno) > 2) $nFiltro=1;
if(strlen($cNombre) > 2) $nFiltro=1;
if(strlen($curp) > 2) $nFiltro=1;
if(strlen($cFolio) > 2) $nFiltro=1;

$idUnidad = $_SESSION['idunidad'];

if($idUnidad != 33){
$anex = "c.id_laboratorio = ".$_SESSION['idunidad'];
}else{
$anex = " b.id_principal > 0 ";
$t=8;
}

/*$sql="select catalogos.dbo.fUnidad_proced(a.id_sector,a.id_unidad) as Unidad, a.cNombre+' '+a.cPaterno+' '+a.cMaterno as Paciente, a.curp,a.nStatus,b.fIngreso, a.id_principal as id_g,c.cFolio as cFolio ,b.nTipoPaciente as nTipoPaciente, b.id_principal as id_reg, a.id_unidad as iduni, a.id_sector as idse, a.nEntidad as idde, d.id_principal as id_lab 
from dat_generales a
inner join dat_registro b on (a.id_principal = b.id_generales)
inner join aux_muestras c on (b.id_principal = c.id_principal)
inner join dat_laboratorio d on (a.id_principal = d.id_generales)";*/

$sql ="select distinct catalogos.dbo.fUnidad_proced(a.id_sector,a.id_unidad) as Unidad,
 a.cNombre+' '+a.cPaterno+' '+a.cMaterno as Paciente,
  a.curp,a.nStatus,b.fIngreso, a.id_principal as id_g,
  c.cFolio as cFolio ,b.nTipoPaciente as nTipoPaciente,
   b.id_principal as id_reg, a.id_unidad as iduni, 
   a.id_sector as idse, a.nEntidad as idde, 
   c.id_principal as id_lab
    from dat_generales a 
    inner join dat_registro b on (a.id_principal = b.id_generales) 
    inner join (
    select a.id_registro, b.cFolio, a.id_principal, a.id_laboratorio from dat_laboratorio a
	inner join aux_muestras b on (a.id_registro = b.id_principal and a.nTipoMuestra = b.nTipoMuestra)
	
    ) c on (b.id_principal = c.id_registro)";

$sql .= " where $anex ";


if($cPaterno != ''){
$sql .=" and cPaterno like '%$cPaterno%'";}
if($cMaterno != ''){
$sql .=" and cMaterno like '%$cMaterno%'";}
if($cNombre != ''){
$sql .=" and cNombre like '%$cNombre%'";}
if($curp != ''){
$sql .=" and curp like '%$curp%'";}
if($cFolio != ''){
$sql .=" and c.cFolio = '$cFolio'";}

//echo $sql;
if($nFiltro==1){
	$qry=mssql_query($sql,$link) or die(mssql_get_last_message());
	$total=mssql_num_rows($qry);
}
?>
<style type="text/css">
<!--
.Estilo1 {font-weight: bold}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
<table width='750' align='center' cellpadding='2' cellspacing='2'  border='0' class='cuadro2 datos'>
  <?php 
  if($total > 0){
  
  ?> <tr >
 
    <td height="23" colspan="7" bgcolor="#F8B145" class="nombres estilo1">
    <b>2. LISTA DE COINCIDENCIAS.</b></td>
  </tr>
  <tr bgcolor='#CCCCCC'>
  <td class="datos"><strong>UNIDAD M&Eacute;DICA</strong></td>
    <td class="datos"><strong>PACIENTE</strong></td>
	<td align="center"  class="datos"><strong>CURP</strong></td>
    <td align="center" class="datos"><strong>INFORMACI&Oacute;N DEL PACIENTE</strong></td>
    <td align="center" class="datos"><strong>FECHA DE INGRESO</strong></td>
    <td align="center" class="datos"><strong>CEDULA DEL PACIENTE</strong></td>
    <td align="center" class="datos"><strong>FOLIO</strong></td>
  </tr>
<?php
while($datos = mssql_fetch_array($qry)){



	
	$fondo = ($fondo=='#f3f3f3'?'#FFFFFF':'#f3f3f3');
	echo "<tr bgcolor='$fondo'>";
	for($i=0;$i<=6;$i++){
		echo "<td class='datos' align='center'>";
		if($i==1){
		$datos_ = "<a href='#' onclick='liga(\"lab_registro.php?id_principal=$datos[id_lab]&t=$t\",\"inicio\")'>".$datos[$i]."</a>"; 
		}
		else if($i==3)
			{ 
				if($datos["nStatus"] == 1 ){
				$datos_ = "COMPLETA"; }else{
				$datos_ = "INCOMPLETA";}	
			}
		else if($i==4)
			{ 
				$datos_ = convertdate5($datos["fIngreso"]); 	
			}
			else if($i==5){ 
	$datos_ = "<a href='imp_cedula.php?id_reg=".$datos["id_reg"]."&id_principal=".$datos["id_g"]."' target='_blank'>ver cedula</a>";
		}
		else if($i==6){ 
	$datos_ = "<a href='imp_ficha.php?lav=3&id_principal=".$datos["id_g"]."&cFolio=".$datos["cFolio"]."' target='_blank'>".$datos["cFolio"]."</a>";
		}
		else {
		$datos_ = $datos[$i];
		}
		echo $datos_;
		echo "</td>";
	}
	echo "</tr>";
}
}
else
{

?> 
<tr >
 
    <td height="25" colspan="6"  align="center" class="nombres estilo1">
  <b>El paciente que busca no existe</b></td>
  </tr>
  <?php }?>
</table>





